import { pathInMenuTree } from './utils/menuUtils';

/**
 * @see https://umijs.org/zh-CN/plugins/plugin-access
 * */
export default function access({ currentUser, menuTree }: System.InitialStatusType = {}) {
  return {
    adminRouteFilter: () => currentUser?.roles?.includes(1) || false, // 只有管理员可访问
    normalRouteFilter: (route: { path: string }) => {
      // initialState 中包含了的路由才有权限访问
      if (!route || !route.path || !menuTree) {
        return false;
      }
      return pathInMenuTree(route.path, menuTree);
    },
    permissionFilter: (permission: string) => {
      if (permission === '') {
        return true;
      }
      return currentUser?.permissions?.includes(permission) || false;
    },
  };
}
